package cn.cxy.demo.viewmodel

import androidx.lifecycle.MutableLiveData
import cn.cxy.demo.base.BaseViewModel
import cn.cxy.demo.network.ArticleListRepo
import cn.cxy.demo.network.response.Article

class ArticleListViewModel : BaseViewModel() {
    private val articleListRepo by lazy { ArticleListRepo() }
    var dataList: MutableLiveData<List<Article>> = MutableLiveData()

    /**
     * @param onSuccess 主要执行代码块
     * @param onFailure 异常处理代码块
     * @param onComplete 无论是否异常都执行的代码块
     */
    fun loadArticleList(onSuccess: () -> Unit, onFailure: ((throwable: Throwable) -> Unit)? = null, onComplete: (() -> Unit)? = null) {
        launch(
                {
                    onSuccess()
                    dataList.value = articleListRepo.getArticleList().apiData().datas
                },
                { onFailure?.invoke(it) },
                { onComplete?.invoke() })
    }
}